package game.jabberserver;

import game.MainGame.ServerGUI;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
/**
 * Server accepts new clients for the game.
 * 
 */
public class Server {

	private int port;
	private boolean keepGoing;
	private SimpleDateFormat sdf; 
	private ServerGUI serverGUI;
	private int ID = 0;
	private int playerMove = 0;
	private boolean someonWon= false;
	private boolean started = false;
	public void setStarted(boolean started) {
		this.started = started;
	}

	public boolean isStarted() {
		return started;
	}
	
	public boolean isSomeonWon() {
		return someonWon;
	}
	
	public void setSomeonWon(boolean someonWon) {
		this.someonWon = someonWon;
	}

	public int getPlayerMove() {
		return playerMove;
	}
	//Increases player move for turns.
	public void setPlayerMove(int playerMove) {
		playerMove++;
		if (playerMove > (playing.size() - 1)) {
			playerMove = 0;
		}
		this.playerMove = playerMove;
	}

	public int getID() {
		return ID;
	}

	public ServerGUI getServerGUI() {
		return serverGUI;
	}

	private ArrayList<ClientSession> clients;
	private ArrayList<ClientSession> playing = null;
	public ArrayList<ClientSession> getPlaying() {
		return playing;
	}

	public void setPlaying(ArrayList<ClientSession> playing) {
		this.playing = playing;
	}

	public void setClients(ArrayList<ClientSession> clients) {
		this.clients = clients;
	}

	public ArrayList<ClientSession> getClients() {
		return clients;
	}

	public Server(int port, ServerGUI sGUI) {
		this.port = port;
		serverGUI = sGUI;
		sdf = new SimpleDateFormat("HH:mm:ss");
		clients = new ArrayList<ClientSession>();
	}
	/**
	 * Starts to accepting clients.
	 * @throws IOException
	 */
	public void start() throws IOException {
		keepGoing = true;
		try {
			ServerSocket serv = new ServerSocket(port);
			while (keepGoing) {
				display("Server waiting for Clients on port " + port + ".");
				Socket sock = serv.accept();
				if (!keepGoing)
					break;
				ID++;
				new ClientSession(sock, this);
				if (clients.size() == 5) {
					while(keepGoing){
						if (!keepGoing)
						break;
					}
				}

			}

			try {
				serv.close();
				//closes all clients's connections
				for (int i = 0; i < clients.size(); ++i) {
					ClientSession client = clients.get(i);
					try {				
						client.getNetIn().close();
						client.getNetOut().close();
						client.getSocket().close();
					} catch (IOException e) {
					}
				}
			} catch (IOException e) {
				display("Exception closing the server and clients: " + e);
			}

		} catch (IOException e) {
			String msg = sdf.format(new Date())
					+ " Exception on new ServerSocket: " + e + "\n";
			display(msg);
		}
	}

	void display(String msg) {
		String time = sdf.format(new Date()) + " " + msg;
		serverGUI.appendEvent(time + "\n");
	}

	/**
	 * Stops the server.
	 */
	public void stop() {
		keepGoing = false;
		try {
			new Socket("localhost", port);
		} catch (Exception e) {}
	}

}
